﻿using CubeCube.Tile;
using CubeCube.Utils;
using CubeCube.World.Generator;

namespace CubeCube.World.Generator
{
    /*
     * 从fromYRV到toYRV用tileType直接填满,只填充空tile
     */
    public class PlainWorldGenerator : CubeWorldGenerator
    {
        private WorldSizeRelativeValue fromYRV;
        private WorldSizeRelativeValue toYRV;
        private byte tileType;

        public PlainWorldGenerator(
            WorldSizeRelativeValue fromYRV,
            WorldSizeRelativeValue toYRV,
            byte tileType)
        {
            this.fromYRV = fromYRV;
            this.toYRV = toYRV;
            this.tileType = tileType;
        }

        public override bool Generate(Chunk.Chunk chunk)
        {
            int fromY = fromYRV.EvaluateInt(chunk);
            int toY = toYRV.EvaluateInt(chunk);

            TileManager tileManager = chunk.tileManager;

            for (int x = 0; x < tileManager.sizeX; x++)
                for (int z = 0; z < tileManager.sizeZ; z++)
                    for (int y = fromY; y < toY; y++)
                    {
                        IndexPosition worldPos = new IndexPosition(x , y, z) + chunk.tileOffset;
                        if (tileManager.GetTileType(worldPos) == TileDefinition.EMPTY_TILE_TYPE)
                            tileManager.SetTileType(worldPos, tileType);
                    }

            return true;
        }

        public override string ToString()
        {
            return "Filling terrain";
        }
    }
}
